package com.spider.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.ProtocolException;

public class CatchUtil {
	
	public final static String METHOD_GET = "GET";
	public final static String METHOD_POST = "POST";
	
	/**
	 * 读取网页内容
	 * @param input
	 * @return
	 */
	public static String bufferRead(InputStream input){
		BufferedReader read;
		try {
			read = new BufferedReader(new InputStreamReader(input,"utf-8"));
			String str = null;
			StringBuffer buff = new StringBuffer();
			while ((str = read.readLine()) != null) {
				buff.append(str);
				buff.append("\n");
			}
			return buff.toString();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public static HttpURLConnection setRequestProperty(HttpURLConnection conn){
		try {
			conn.setRequestMethod(METHOD_GET);
			conn.setRequestProperty("Host", "bj.ganji.com");
			conn.setRequestProperty("Connection", "keep-alive");
			conn.setRequestProperty("Cache-Control", "max-age=0");
			conn.setRequestProperty("Accept",
							"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			conn.setRequestProperty("User-Agent",
							"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36");
			conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
			return conn;
		} catch (ProtocolException e) {
			e.printStackTrace();
		}
		
		return null;
	}
}
